﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wsitech.BLL;
using Wsitech.Model;

public partial class Backend_Admin_RoleAdd : BackendPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindSitemap();
            if (Request["rid"] != null)
            {
                initRole(Request["rid"]);
            }
        }
    }

    //绑定后台菜单
    private void bindSitemap()
    {
        repParentMenu.DataSource = SiteMapInfoBLL.Instance.GetBackendParentList();
        repParentMenu.DataBind();
    }

    //初始化
    private void initRole(string roleid)
    {
        AdminRole role = AdminRoleBLL.Instance.GetDetail(roleid);
        if (role != null)
        {
            txtRoleName.Text = role.RoleName;
            txtDescription.Text = role.Description;
            hfFunIdList.Value = string.Join(",", RoleFuncRelaBLL.Instance.GetListByRoleId(roleid).Select(r => r.FuncId));
        }
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (txtRoleName.Text.Trim() == "")
        {
            base.Alert("角色名称不能为空！");
            return;
        }
        string[] funList = { };
        if (hfFunIdList.Value == "")
        {
            base.Alert("请为该角色选择权限！");
            return;
        }
        else
        {
            funList = hfFunIdList.Value.Split(',');
        }

        if (Request["rid"] == null)
        {
            AdminRoleBLL.Instance.AddRole(txtRoleName.Text.Trim(), txtDescription.Text.Trim(), funList);
        }
        else
        {
            AdminRoleBLL.Instance.UpdateRole(Request["rid"], txtRoleName.Text.Trim(), txtDescription.Text.Trim(), funList);
        }

        base.RefreshParent();
    }
}